local joyex__shangshi = fk.CreateSkill {

  name = "joyex__shangshi",

  tags = { Skill.Compulsory, },

}



joyex__shangshi:addEffect(fk.HpChanged, {
  name = "joyex__shangshi",
  anim_type = "drawcard",
  events = {fk.HpChanged, fk.MaxHpChanged, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joyex__shangshi.name) and (player:getHandcardNum() < player:getLostHp() or player:getHandcardNum() < 1) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          return move.from==player
        end
      else
        return target == player
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = math.max(player:getLostHp(),1)
    return player.room:askForSkillInvoke(player, joyex__shangshi.name, nil, "#joyex__shangshi-invoke1:::"..n)
  end,
  on_use = function(self, event, target, player, data)
    local n = math.max(player:getLostHp(),1)
    local m = n - player:getHandcardNum()
    player:drawCards(m, joyex__shangshi.name)
    if player:getMark("joyex__jueqing_discard-turn") == 0 and not player:isKongcheng() then
      local hand = player:getCardIds(Player.Hand)
      local card = player.room:askForCard(player, 1, 999, false, joyex__shangshi.name, true, ".", "#joyex__shangshi-invoke2")
      if #card > 0 then
        player:setMark("joyex__jueqing_discard-turn",1)
        if #card == #hand then
          player.room:addPlayerMark(player,"@joyex__shangshi",1)
        end
        player.room:throwCard(card, joyex__shangshi.name, player, player)
      end
    end
  end,
})
joyex__shangshi:addEffect(fk.MaxHpChanged, {
  name = "joyex__shangshi",
  anim_type = "drawcard",
  events = {fk.HpChanged, fk.MaxHpChanged, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joyex__shangshi.name) and (player:getHandcardNum() < player:getLostHp() or player:getHandcardNum() < 1) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          return move.from==player
        end
      else
        return target == player
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = math.max(player:getLostHp(),1)
    return player.room:askForSkillInvoke(player, joyex__shangshi.name, nil, "#joyex__shangshi-invoke1:::"..n)
  end,
  on_use = function(self, event, target, player, data)
    local n = math.max(player:getLostHp(),1)
    local m = n - player:getHandcardNum()
    player:drawCards(m, joyex__shangshi.name)
    if player:getMark("joyex__jueqing_discard-turn") == 0 and not player:isKongcheng() then
      local hand = player:getCardIds(Player.Hand)
      local card = player.room:askForCard(player, 1, 999, false, joyex__shangshi.name, true, ".", "#joyex__shangshi-invoke2")
      if #card > 0 then
        player:setMark("joyex__jueqing_discard-turn",1)
        if #card == #hand then
          player.room:addPlayerMark(player,"@joyex__shangshi",1)
        end
        player.room:throwCard(card, joyex__shangshi.name, player, player)
      end
    end
  end,
})
joyex__shangshi:addEffect(fk.AfterCardsMove, {
  name = "joyex__shangshi",
  anim_type = "drawcard",
  events = {fk.HpChanged, fk.MaxHpChanged, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(joyex__shangshi.name) and (player:getHandcardNum() < player:getLostHp() or player:getHandcardNum() < 1) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          return move.from==player
        end
      else
        return target == player
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = math.max(player:getLostHp(),1)
    return player.room:askForSkillInvoke(player, joyex__shangshi.name, nil, "#joyex__shangshi-invoke1:::"..n)
  end,
  on_use = function(self, event, target, player, data)
    local n = math.max(player:getLostHp(),1)
    local m = n - player:getHandcardNum()
    player:drawCards(m, joyex__shangshi.name)
    if player:getMark("joyex__jueqing_discard-turn") == 0 and not player:isKongcheng() then
      local hand = player:getCardIds(Player.Hand)
      local card = player.room:askForCard(player, 1, 999, false, joyex__shangshi.name, true, ".", "#joyex__shangshi-invoke2")
      if #card > 0 then
        player:setMark("joyex__jueqing_discard-turn",1)
        if #card == #hand then
          player.room:addPlayerMark(player,"@joyex__shangshi",1)
        end
        player.room:throwCard(card, joyex__shangshi.name, player, player)
      end
    end
  end,
})

joyex__shangshi:addEffect(fk.DrawNCards, {
  name = "#joyex__shangshi_drawcard",
  anim_type = "negative",
  events = {fk.DrawNCards,fk.EventPhaseChanging},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseChanging then
      return player:hasSkill(joyex__shangshi.name) and target == player and player:getMark("@joyex__shangshi") > 0
    elseif event == fk.DrawNCards then
      return  player:hasSkill(joyex__shangshi.name) and target == player and player:getMark("@joyex__shangshi") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local m = player:getMark("@joyex__shangshi")
    if event == fk.DrawNCards then
      data.n = data.n + m
    end
    player.room:removePlayerMark(player, "@joyex__shangshi", m)
  end,
})
joyex__shangshi:addEffect(fk.EventPhaseChanging, {
  name = "#joyex__shangshi_drawcard",
  anim_type = "negative",
  events = {fk.DrawNCards,fk.EventPhaseChanging},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseChanging  then
      return player:hasSkill(joyex__shangshi.name) and target == player and player:getMark("@joyex__shangshi") > 0
    elseif event == fk.DrawNCards then
      return  player:hasSkill(joyex__shangshi.name) and target == player and player:getMark("@joyex__shangshi") > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local m = player:getMark("@joyex__shangshi")
    if event == fk.DrawNCards then
      data.n = data.n + m
    end
    player.room:removePlayerMark(player, "@joyex__shangshi", m)
  end,
})

return joyex__shangshi